// 点赞和取消点赞
<template>
  <van-button
    style="border:none"
    :icon="value ? 'star' : 'star-o'"
    :class="{ collected: value }"
    @click="onCollect"
    :loading="loading"
  ></van-button>
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article.js'
// console.log(addCollect, deleteCollect)
export default {
  name: 'CollectArticle',
  components: {},
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    articleId: {
      type: [Number, String],
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onCollect() {
      this.loading = true
      try {
        // 如果是已经点赞，那么久取消点赞
        if (this.value) {
          await deleteCollect(this.articleId)
        } else {
          // 没有收藏，要添加收藏
          await addCollect(this.articleId)
        }
        this.$toast.success(this.value ? '取消收藏' : '收藏')
        // 给父级的@input 传值
        this.$emit('input', !this.value)
      } catch (err) {
        this.$toast.fail('操作失败，请重试！')
      }
      this.loading = false
    },
  },
}
</script>

<style lang="less" scoped>
.collected {
  .van-icon {
    color: #ffa500;
  }
}
</style>
